<?php

namespace app\user\controller;

use app\model\UserExpressLog as ModelUserExpressLog;
use app\model\Express as ModelExpress;
use app\user\logic\Express as UserLogicExpress;
use app\user\logic\User as UserLogicUser;

/**
 * 快递相关类
 * Class Common
 * @package app\user\controller
 */
class Express extends Member
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 获取快递信息列表 \n
     * URI : /express.list.get
     * @param :
     *  name     |  type  | null | description
     * ----------|--------|------|-------------
     *  passport | string | 必填  | 登录凭证
     *
     * @return
     *  name        |  type  | description
     * -------------|--------|-------------
     *  list        | array  | 列表
     *
     * list
     *  name        |  type  | description
     * -------------|--------|-------------
     *  expressName | string | 快递公司名
     *  expressLogo | string | 快递公司logo
     *
     */
    public function getExpressList()
    {
        $arr_list = ModelExpress::getInstance()->toSelect([], 'express_name AS expressName,express_logo AS expressLogo');
        _returnData(['list' => $arr_list]);
    }

    /**
     * 快递查询结果 \n
     * URI : /express.inquiry
     * @param :
     *  name      |  type  | null | description
     * -----------|--------|------|-------------
     *  num       | string | 必填  | 快递单号
     *  expressCom|  int   | 选填  | 公司简称
     *
     * @return
     *  name        |  type  | description
     * -------------|--------|-------------
     *  num         | string | 快递单号
     *  com         | string | 快递公司简写
     *  state       | string | 物流状态
     *  data        | array  | 物流信息
     *  logo        | string | 快递公司logo
     *  name        | string | 快递公司名
     *  phone       | string | 联系号码
     *
     * data
     *  name         |  type  | description
     * --------------|--------|-------------
     *  time         | string | 每条跟踪信息的时间
     *  context      | string | 每条跟综信息的描述
     *
     * @note
     * state状态码信息:
     * 0：在途，即货物处于运输过程中；
     * 1：揽件，货物已由快递公司揽收并且产生了第一条跟踪信息；
     * 2：疑难，货物寄送过程出了问题；3：签收，收件人已签收；
     * 4：退签，即货物由于用户拒签、超区等原因退回，而且发件人已经签收；
     * 5：派件，即快递正在进行同城派件；
     * 6：退回，货物正处于退回发件人的途中
     */
    public function expressInquiry()
    {
        $str_num = $this->_getContentParams('num');//单号
        _toValidate('math', $str_num, 12002);

        $str_com = $this->_getContentParams('expressCom');//快递公司简称

        //快递查询
        $arr_info = UserLogicExpress::getInstance()->express($str_num, $str_com);
        _toCompare($arr_info, '===', false, 12001);

        //快递公司信息
        $arr_express_info = ModelExpress::getInstance()->toFind(['express_com' => $arr_info['com']], 'express_id,express_logo,express_name,express_phone');

        $arr_info['logo'] = isset($arr_express_info['express_logo']) ? (string)$arr_express_info['express_logo'] : '';
        $arr_info['name'] = isset($arr_express_info['express_name']) ? (string)$arr_express_info['express_name'] : '';
        $arr_info['phone'] = isset($arr_express_info['express_phone']) ? (string)$arr_express_info['express_phone'] : '';

        UserLogicUser::getInstance()->addExpressLog($this->user_id, (int)$arr_express_info['express_id'], $str_num); //生成历史记录

        _returnData(['info' => $arr_info]);
    }

    /**
     * 快递查询历史记录 \n
     * URI : /express.user.log
     * @param :
     *  name     |  type  | null | description
     * ----------|--------|------|-------------
     *  passport | string | 必填  | 登录凭证
     *  page     |  int   | 必填  | 页码
     *  listRows |  int   | 必填  | 条数
     *
     * @return
     *  name        |  type  | description
     * -------------|--------|-------------
     *  totalPages  | string | 总页数
     *  list        | array  | 列表
     *
     * list
     *  name         |  type  | description
     * --------------|--------|-------------
     *  logId        | int    | 记录id
     *  courierNumber| string | 单号
     *  expressName  | string | 快递公司名
     *  expressLogo  | string | 快递公司logo
     *  expressCom   | string | 快递公司简称
     */
    public function userExpressLog()
    {
        $int_now_page = 1;//$this->_getContentParams('page');//当前页码
        _toValidate('id', $int_now_page, 10005);

        $int_list_rows = 30;//$this->_getContentParams('listRows');//一页的条数
        _toValidate('id', $int_list_rows, 10006);

        $int_total_rows = ModelUserExpressLog::getInstance()->toCount(['user_id' => $this->user_id]);//总条数
        $int_total_pages = ceil($int_total_rows / $int_list_rows);//总页数
        _toCompare(($int_now_page > $int_total_pages && $int_total_pages > 0), '==', true, 10007);

        $int_first_row = ($int_now_page - 1) * $int_list_rows;

        //历史总列表
        $arr_log_list = (array)ModelUserExpressLog::getInstance()->toSelect(
            ['user_id' => $this->user_id],
            'log_id as logId,courier_number as courierNumber,express_id',
            $int_first_row,
            $int_list_rows,
            'gmt_create desc'
        );

        //快递公司相关数据
        $arr_express_list = (array)ModelExpress::getInstance()->toColumn(
            ['express_id' => ['in', implode(',', array_column($arr_log_list, 'express_id'))]],
            'express_name,express_logo,express_com',
            'express_id'
        );

        foreach ($arr_log_list as &$value) {
            $value['expressName'] = isset($arr_express_list[$value['express_id']]['express_name']) ? $arr_express_list[$value['express_id']]['express_name'] : ''; //快递公司名
            $value['expressLogo'] = isset($arr_express_list[$value['express_id']]['express_logo']) ? $arr_express_list[$value['express_id']]['express_logo'] : ''; //快递公司logo
            $value['expressCom'] = isset($arr_express_list[$value['express_id']]['express_com']) ? $arr_express_list[$value['express_id']]['express_com'] : ''; //快递公司简称
            unset($value['express_id']);
        }

        _returnData(['list' => (array)$arr_log_list]);
        //_returnData(['totalPages' => (int)$int_total_pages, 'list' => (array)$arr_log_list]);
    }


    /**
     * 删除快递查询历史记录 \n
     * URI : /express.user.log
     * @param :
     *  name     |  type  | null | description
     * ----------|--------|------|-------------
     *  passport | string | 必填  | 登录凭证
     *  logId    |  int   | 必填  | 记录id
     *
     * @return
     *  name        |  type  | description
     * -------------|--------|-------------
     *  ----------  | ------ | 无
     */
    public function delUserExpressLog()
    {
        $int_log_id = $this->_getContentParams('logId');//id
        _toValidate('id', $int_log_id, 10009);

        //删除
        $res = ModelUserExpressLog::getInstance()->toDelete(['log_id' => $int_log_id, 'user_id' => $this->user_id]);

        if ($res) _returnData();
        _returnError(10012);
    }
}
